package com.example.demo.common;

import cn.hutool.core.lang.hash.Hash;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListNode {
    public int value;
    public ListNode next;
    public ListNode head;

    public ListNode(int value) {
        this.value = value;
    }
    // 反转链表
    public ListNode reverseList() {
        if (head == null) return null;
        if (head.next == null) return head;
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            // 头插法
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    // 返回链表的中间节点
    public ListNode middleNode() {
        if (head == null) return null;
        ListNode s = head;
        ListNode f = head;
        while (f != null && f.next != null) {
            f = f.next.next;
            s = s.next;
        }
        return s;
    }
    // 返回链表的倒数第 k 个节点
    public ListNode findKthTo0Tail(int k) {
        if (k < 0 || head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while (k != 1) {
            fast = fast.next;
            if (fast == null) return null;
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    // 合并两个有序链表
    public ListNode mergeList(ListNode list1, ListNode list2) {
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (list1 != null && list2 != null) {
            if (list1.value < list2.value) {
                tmp.next = list1;
                list1 = list1.next;
            } else {
                tmp.next = list2.next;
                list2 = list2.next;
            }
            tmp = tmp.next;
        }
        if (list1 == null) tmp.next = list2;
        if (list2 == null) tmp.next = list1;
        return tmp.next;
    }

    // 判断链表是否是回文结构
    public boolean isPail() {
        if (head == null) return false;
        if (head.next == null) return true;
        //1. 找到中间节点，反转
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        // 2. 判断值是否相等
        while (slow != head) {
            if (slow.value != head.value) return false;
            if (head.value == head.next.value) return true;
            slow = slow.next;
            head = head.next;
        }
        return true;
    }
    // 链表分割
    public ListNode partition(int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while (cur != null) {
            if (cur.value < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) return as;
        be.next = as;
        if (as != null) ae.next = null;
        return bs;
    }
    // 相交链表
    public ListNode getIntersectionNode(ListNode head1, ListNode head2) {
        // 1. 先求长度
        ListNode cur1 = head1, cur2 = head2;
        int len1 = 0, len2 = 0;
        while (cur1 != null) {
            len1++;
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            len2++;
            cur2 = cur2.next;
        }
        cur1 = head1;
        cur2 = head2;
        // 2. 然后走差值步数
        int length = Math.abs(len1 - len2);
        while (length != 0) {
            if (len1 < len2) cur2 = cur2.next;
            else cur1 = cur1.next;
        }
        // 3. 最后两个指针一起走看能否相遇
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur2;
    }
    // 判断链表是否有环
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) return true;
        }
        return false;
    }
    // 环形链表：返回链表开始入环的第一个节点




    /**
     * 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的，但不知道有几个数字是重复的。
     * 也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如，如果输入长度为7的数组[2,3,1,0,2,5,3]，
     * 那么对应的输出是2或者3。
     * 存在不合法的输入的话输出-1
     */
    private static int duplicate(int[] nums) {
        Map<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.get(nums[i]) == null) {
                hashMap.put(nums[i], 1);
            } else {
                return nums[i];
            }
        }
        return -1;
    }
}

/*学生表student中有如下几列：id（学号）、班级（class_id）、
年龄（age）、性别（gender），要求查出每个班级中年龄大于18岁的女生的数量
* select count(id) from student where age > 18 and gender = '女' group by class_id;
*/
